package com.ksm.version.service.impl;

import com.ksm.common.constant.Constants;
import com.ksm.common.utils.SecurityUtils;
import com.ksm.common.utils.ServletUtils;
import com.ksm.framework.web.service.TokenService;
import com.ksm.version.domain.SysDeployHistory;
import com.ksm.version.domain.SysHistoryAndPackageVersion;
import com.ksm.version.domain.SysServerAndPackageVersion;
import com.ksm.version.domain.SysVersionManage;
import com.ksm.version.domain.vo.ProjectVo;
import com.ksm.version.domain.vo.VersionVo;
import com.ksm.version.mapper.ProjectMapper;
import com.ksm.version.mapper.VersionMapper;
import com.ksm.version.service.VersionService;
import com.ksm.version.util.OpenSSHClient;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Optional;

@Service
public class VersionServiceImpl implements VersionService {


    @Autowired(required = false)
    private ProjectMapper mapper;
    @Autowired
    private VersionMapper versionMapper;

    private OpenSSHClient scpclient;

    @Autowired
    private TokenService tokenService;

    @Override
    public boolean updateVersion(VersionVo versionVo) {
        SysVersionManage manage = mapper.findOne(versionVo.getVersionId());
        if (manage.getCurrentVersion().equals(manage.getPackageVersion())) {
            return manage.getSysDeployHistory().getDeployDate().before(manage.getUpdatedTime());
        } else {
            return true;
        }
    }

    /**
     * 平台手动远程更新
     *
     * @param versionVo
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Integer windowsTriggerUpdateVersion(VersionVo versionVo) {

        SysVersionManage manage = mapper.findOne(versionVo.getVersionId());
        scpclient = new OpenSSHClient(
                manage.getSysServer().getServerIp(),
                manage.getSysServer().getServerPort(),
                manage.getSysServer().getServerUserName(),
                manage.getSysServer().getServerPassword()
        );
        scpclient.putFile(Constants.DEFAULT_COMMAND_WINDOWS, Constants.DEFAULT_SCRIPT_NAME, Constants.DEFAULT_SAVE_FOLDER, null);
        scpclient.putFile(manage.getUrl(), versionVo.getRealName(), Constants.DEFAULT_SAVE_FOLDER, null);
        scpclient.execCommand(Constants.DEFAULT_COMMAND_WINDOWS);
        mapper.updateDeployHistory(manage.getSysDeployHistory().getId());
        Date date = versionMapper.findOne(manage.getSysDeployHistory().getId()).getDeployDate();
        SysVersionManage manage1 = new SysVersionManage();
        manage1.setDeployId(manage.getDeployId());
        manage1.setPackageVersion(manage.getCurrentVersion());
        manage1.setUpdatedTime(date);
        return mapper.updateTime(manage1);


    }



    /**
     * 一次部署
     *
     * @param projectVo 项目相关信息
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Integer windowsClientAdd(ProjectVo projectVo) {


        SysVersionManage manage = mapper.findOne(projectVo.getProjectDevelopmentId());
        scpclient = new OpenSSHClient(
                manage.getSysServer().getServerIp(),
                manage.getSysServer().getServerPort(),
                manage.getSysServer().getServerUserName(),
                manage.getSysServer().getServerPassword()
        );
        scpclient.putFile(Constants.DEFAULT_COMMAND_WINDOWS, Constants.DEFAULT_SCRIPT_NAME, Constants.DEFAULT_SAVE_FOLDER, null);
        scpclient.putFile(manage.getUrl(), projectVo.getProjectFileName(), Constants.DEFAULT_SAVE_FOLDER, null);
        scpclient.execCommand(Constants.DEFAULT_COMMAND_WINDOWS);
        SysDeployHistory deployHistory = new SysDeployHistory();
        deployHistory.setAppName(manage.getPackageName());
        deployHistory.setDeployUser(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserName());
        versionMapper.addNewDeployHistory(deployHistory);
        mapper.updateRemoteDep(projectVo.getProjectDevelopmentId());
        return versionMapper.addNewDeployHistoryMid(new SysHistoryAndPackageVersion(deployHistory.getId(), manage.getDeployId()));
    }

    /**
     * 客户端手动更新
     * @param versionVo
     * @return
     */
    @Override
    public Integer windowsClientUpdate(VersionVo versionVo) {
        return null;
    }
}
